package licode.unisop.log.service.infrastructure.repository.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import licode.unisop.log.client.api.CallingLogApi;
import licode.unisop.log.client.dto.CallingLogDTO;
import licode.unisop.log.client.req.CallingLogIn;
import licode.unisop.log.service.infrastructure.assembler.LogAssembler;
import licode.unisop.log.service.infrastructure.repository.mp.mapper.CallApiLogMapper;
import licode.unisop.log.service.infrastructure.repository.po.CallingLogPO;
import licode.unisop.types.util.IdGenUtil;
import licode.unisop.unilite.dp.PageData;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WR
 * @since 2016/7/9 16:05
 */
@Primary
@Component
public class CallingLogApiImpl
        extends ServiceBase<CallApiLogMapper, CallingLogPO>
        implements CallingLogApi {
    @Resource
    private LogAssembler logAssembler;

    @Override
    public void save(CallingLogDTO source) {
        CallingLogPO dest = logAssembler.fromCallApiLogDto(source);
        dest.setId(IdGenUtil.buildCallApiLogId());
        super.save(dest);
    }

    @Override
    public void saveList(List<CallingLogDTO> source) {
        List<CallingLogPO> dest = logAssembler.fromCallApiLogDto(source);
        for (CallingLogPO item: dest) {
            item.setId(IdGenUtil.buildCallApiLogId());
            super.save(item);
        }
    }

    @Override
    public PageData<CallingLogDTO> query(CallingLogIn req) {
        Page<CallingLogPO> page = new Page<>(req.getCurrent(),
                req.getPageSize());
        String type = req.getType() != null ? req.getType() : "";

        page = super.lambdaQuery()
                .eq(StrUtil.isNotBlank(req.getPlatformId()),
                        CallingLogPO::getPlatformId, req.getPlatformId())
                .eq(StrUtil.isNotBlank(req.getType()),
                        CallingLogPO::getType, type.toUpperCase())
                .like(StrUtil.isNotBlank(req.getPlatformName()),
                        CallingLogPO::getPlatformName, req.getPlatformName())
                .like(StrUtil.isNotBlank(req.getClientId()),
                        CallingLogPO::getClientId, req.getClientId())
                .like(StrUtil.isNotBlank(req.getParam()),
                        CallingLogPO::getParam, req.getParam())
                .like(StrUtil.isNotBlank(req.getUrl()),
                        CallingLogPO::getUrl, req.getUrl())
                .le(null != req.getEndDate(),
                        CallingLogPO::getCallTime, req.getEndDate())
                .ge(null != req.getStartDate(),
                        CallingLogPO::getCallTime, req.getStartDate())
                .orderByDesc(CallingLogPO::getCallTime)
                .page(page);

        return convertPage(page, (List<CallingLogPO> data) ->
                logAssembler.fromCallApiLog(data));
    }
}
